import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.CreateImageRequest;
import com.amazonaws.services.ec2.model.CreateImageResult;
import com.amazonaws.services.ec2.model.DescribeImagesRequest;
import com.amazonaws.services.ec2.model.DescribeImagesResult;
import com.amazonaws.services.ec2.model.Image;

public class ImageCreater {
	private String instanceId;
	private AWSCredentials credentials;
	private String imageName;
	
	
	public ImageCreater(String instanceId, AWSCredentials credentials){
		this.instanceId=instanceId;
		this.credentials=credentials;
		
	}
	
	public String setImage(String instanceId, String imageName, AmazonEC2 ec2){
		this.imageName = imageName;
		CreateImageRequest cir = new CreateImageRequest();
		cir.setInstanceId(instanceId);
		cir.setName(imageName);
		CreateImageResult createImageResult = ec2.createImage(cir);
		String createdImageId = createImageResult.getImageId();
	 
		System.out.println("Sent creating AMI request. AMI id="+createdImageId);
		
		
		DescribeImagesRequest dirq = new DescribeImagesRequest();
	    
		DescribeImagesResult dir = ec2.describeImages(dirq);
		Image im = dir.getImages().get(0);
		im.setPublic(true);
		String imageId = im.getImageId();
		return createdImageId;
		
	}
	
	public void setImageName(String imageName){
		this.imageName = imageName;
	}
	
	public String getImageName(){
		return imageName;
	}
	
}